def bfs(graph, start):
    """
    explored: 已遍历的节点列表
    queue: 寻找待遍历的节点队列
    """
    explored, queue = [], [start]
    explored.append(start)
    while queue:
        """
        :v 将要遍历的某节点
        :w 节点v的邻居节点
        """
        v = queue.pop(0)
        for w in graph[v]:
            # 如果w未被遍历，则遍历
            if w not in explored:
                explored.append(w)
                queue.append(w)
    return explored


if __name__ == '__main__':
    G = {'0': ['1', '2'],
         '1': ['2', '3'],
         '2': ['3', '5'],
         '3': ['4'],
         '4': [],
         '5': []}

    print(bfs(G, '0'))
